import React, { Component } from 'react';
import './index.less';
import { base } from 'nc-lightapp-front';
import getEditInitData from './functions/getEditInitData';
import Deptchg from './components/Deptchg';
import Trial from './components/Trial';
import Psnchg from './components/Psnchg';
import Dimission from './components/Dimission';
import Retire from './components/Retire';
import checkEditData from './functions/checkEditData';
import saveEditData from './functions/saveEditData';
import TransInOut from '../CrossTrans/components/TransInOut';

const { NCModal, NCSelect, NCButton, NCTabs, NCTable } = base;
const { Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter } = NCModal;
const { Option: NCOption } = NCSelect;
const NCTabPane = NCTabs.NCTabPane;

class BatchEditModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			activeKey: ''
		};
		this.tabs = [];
		this.getEditInitInfo = this.getEditInitInfo.bind(this);
		this.closeModal = this.closeModal.bind(this);
		this.tabChange = this.tabChange.bind(this);
		this.beSure = this.beSure.bind(this);
		this.saveEditData = this.saveEditData.bind(this);
		this.warningCloseHandle = this.warningCloseHandle.bind(this);
		this.confirmSureHandle = this.confirmSureHandle.bind(this);
		this.confirmCloseHandle = this.confirmCloseHandle.bind(this);
	}

	getEditInitInfo() {
		const { isInJob, orgVal, table, func_type } = this.props;
		this.tabs = [
			{
				key: 'hi_psndoc_dimission',
				text: this.props.json['hi6007-000128'] /* 国际化处理： 人员任职记录*/,
				comp: <Dimission ref={(node) => (this.hi_psndoc_dimission = node)} json={this.props.json} />
			},
			{
				key: 'hi_psndoc_deptchg',
				text: this.props.json['hi6007-000128'] /* 国际化处理： 人员任职记录*/,
				comp: <Deptchg ref={(node) => (this.hi_psndoc_deptchg = node)} json={this.props.json} />
			},
			{
				key: 'hi_psndoc_psnchg',
				text: this.props.json['hi6007-000129'] /* 国际化处理： 人员流动情况*/,
				comp: <Psnchg ref={(node) => (this.hi_psndoc_psnchg = node)} json={this.props.json} />
			},
			{
				key: 'hi_psndoc_trial',
				text: this.props.json['hi6007-000130'] /* 国际化处理： 人员试用情况*/,
				comp: <Trial ref={(node) => (this.hi_psndoc_trial = node)} json={this.props.json} />
			},
			{
				key: 'hi_psndoc_retire',
				text: this.props.json['hi6007-000131'] /* 国际化处理： 人员离职待遇*/,
				comp: <Retire ref={(node) => (this.hi_psndoc_retire = node)} json={this.props.json} />
			}
		];

		const param_str = table.getCheckedRows('info').map((item) => item.data.values.pk_psnjob.value).join();
		if (!param_str) return;
		getEditInitData({
			func_type,
			isInJob,
			param_str,
			pk_org: orgVal.refpk
		}).then((res) => {
			if (res.success) {
				this.tabs = this.tabs.filter((item) => res.data.indexOf(item.key) > -1);
				this.setState({
					activeKey: this.tabs[0].key
				});
			}
		});
	}

	isEmpty(param) {
		let hasvalue = false;
		for (let pro in param) {
			hasvalue = true;
			break;
		}
		return !hasvalue;
	}

	beSure() {
		const { activeKey } = this.state;
		const editInfo = this[activeKey].getInfo();
		if (this.isEmpty(editInfo)) {
			this.closeModal();
		} else {
			const { orgVal, table } = this.props;
			const param_str = table.getCheckedRows('info').map((item) => item.data.values.pk_psnjob.value).join();
			if (!param_str) return;
			checkEditData({
				edit_value: JSON.stringify(editInfo),
				table_code: activeKey,
				param_str,
				pk_org: orgVal.refpk
			}).then((res) => {
				if (res.success) {
					this.editInfo = editInfo;
					this.passPks = res.data.passPks;
					this.confirmMsg = res.data.confirmMsg;
					this.confirmPks = res.data.confirmPks;
					this.worningMsg = res.data.worningMsg;
					if (!this.confirmMsg && !this.worningMsg) {
						this.saveEditData();
					} else {
						this.props.modal.show(this.worningMsg ? 'warningModal' : 'confirmModal');
					}
				}
			});
		}
	}

	saveEditData() {
		if (!this.passPks) {
			this.closeModal();
			this.props.getMainTableData(true);
			return;
		}
		saveEditData({
			edit_value: JSON.stringify(this.editInfo),
			table_code: this.state.activeKey,
			param_str: this.passPks,
			pk_org: this.props.orgVal.refpk
		}).then((res) => {
			if (res.success) {
				this.closeModal();
				this.props.getMainTableData(true);
			}
		});
	}

	warningCloseHandle() {
		const { modal } = this.props;
		modal.close('warningModal');
		if (!this.confirmMsg) {
			this.saveEditData();
		} else {
			modal.show('confirmModal');
		}
	}

	confirmSureHandle() {
		const { modal } = this.props;
		modal.close('confirmModal');
		if (this.passPks) {
			this.passPks = this.passPks.concat(',' + this.confirmPks);
		} else {
			this.passPks = this.confirmPks;
		}
		this.saveEditData();
	}

	confirmCloseHandle() {
		const { modal } = this.props;
		modal.close('confirmModal');
		this.saveEditData();
	}

	closeModal() {
		this.props.onClose && this.props.onClose();
		this.setState({
			activeKey: ''
		});
	}

	tabChange(activeKey) {
		this.setState({
			activeKey
		});
	}

	render() {
		const { visible, modal } = this.props;
		const { activeKey } = this.state;
		const { createModal } = modal;
		return (
			<div>
				<NCModal
					className="batch-edit-modal"
					show={visible}
					size="lg"
					onHide={this.closeModal}
					onEntered={this.getEditInitInfo}
					backdrop={'static'}
				>
					<NCModalHeader closeButton={true}>
						{this.props.json['hi6007-000040']}
						{/* 国际化处理： 批量修改*/}
					</NCModalHeader>
					<NCModalBody>
						<NCTabs
							navtype="turn"
							contenttype="moveleft"
							tabBarPosition="top"
							activeKey={activeKey}
							onChange={this.tabChange}
						>
							{this.tabs.map((item) => (
								<NCTabPane tab={item.text} key={item.key}>
									{item.comp}
								</NCTabPane>
							))}
						</NCTabs>
					</NCModalBody>
					<NCModalFooter>
						<NCButton colors="primary" onClick={this.beSure}>
							{this.props.json['hi6007-000174']}
							{/* 国际化处理： 确认*/}
						</NCButton>
						<NCButton shape="border" onClick={this.closeModal}>
							{this.props.json['hi6007-000005']}
							{/* 国际化处理： 取消*/}
						</NCButton>
					</NCModalFooter>
				</NCModal>
				{createModal('warningModal', {
					title: `${this.props.json['hi6007-000108']}` /* 国际化处理： 提醒*/,
					content: (
						<div
							dangerouslySetInnerHTML={{
								__html: this.worningMsg
							}}
						/>
					),
					size: 'sm',
					backdrop: 'static',
					noFooter: true,
					userControl: true,
					cancelBtnClick: this.warningCloseHandle
				})}

				{createModal('confirmModal', {
					title: this.props.json['hi6007-000174'] /* 国际化处理： 确认,确认*/,
					content: (
						<div
							dangerouslySetInnerHTML={{
								__html: this.confirmMsg
							}}
						/>
					),
					beSureBtnClick: this.confirmSureHandle,
					size: 'sm',
					backdrop: 'static',
					rightBtnName: this.props.json['hi6007-000005'], //左侧按钮名称,默认关闭/* 国际化处理： 取消*/
					leftBtnName: this.props.json['hi6007-000004'], //右侧按钮名称， 默认确认/* 国际化处理： 确定*/
					userControl: true,
					cancelBtnClick: this.confirmCloseHandle
				})}
			</div>
		);
	}
}

export default BatchEditModal;
